////////////////////////////////////////////////////////////////////////////////
/// @file     uart_loop.c
/// @author   AE TEAM
/// @brief    THIS FILE PROVIDES ALL THE SYSTEM FUNCTIONS.
////////////////////////////////////////////////////////////////////////////////
/// @attention
///
/// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
/// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
/// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
/// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
/// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
/// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
///
/// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
////////////////////////////////////////////////////////////////////////////////

// Define to prevent recursive inclusion
#define _UART_LOOP_C_

// Files includes
#include "sys.h"
#include "uart_loop.h"

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup MM32_Hardware_Abstract_Layer
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup UART_LOOP
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @addtogroup UART_LOOP_Exported_Functions
/// @{

////////////////////////////////////////////////////////////////////////////////
/// @brief  uart initialization
/// @param  None
/// @retval None
////////////////////////////////////////////////////////////////////////////////
void UartInit_Loop(s32 bound)
{

    u32 tempBaud;

    RCC->AHBENR |= RCC_AHBENR_GPIOA; //enablePORTA clock
    RCC->APB1ENR |= RCC_APB1ENR_UART1; //enable uart clock
    RCC->APB1RSTR |= RCC_APB1RSTR_UART1; //reset   uart 1
    RCC->APB1RSTR &= ~RCC_APB1RSTR_UART1; //stopreset

    // Determine the uart_baud
    tempBaud = (SystemCoreClock / bound) / 16;
    UART1->FRA = (SystemCoreClock / bound) % 16;
    // Write to UART BRR
    UART1->BRR = tempBaud;
    UART1->CCR |= UART_CCR_CHAR_8b;//| UART_CCR_SPB |  UART_CCR_PSEL |  UART_CCR_PEN; //1bit stop,none verify bit .
    UART1->GCR = UART_GCR_UART | UART_GCR_RX | UART_GCR_TX;

    GPIOA->CRL &= ~(GPIO_CNF_MODE_MASK << GPIO_CRL_CNF_MODE_0_Pos); //IO status set
    GPIOA->CRL |= GPIO_CNF_MODE_FLOATING << GPIO_CRL_CNF_MODE_0_Pos; //IO status set    PA0

    GPIOA->CRH &= ~(GPIO_CNF_MODE_MASK << GPIO_CRH_CNF_MODE_12_Pos); //IO status set
    GPIOA->CRH |= GPIO_CNF_MODE_AF_PP << GPIO_CRH_CNF_MODE_12_Pos; //IO status set    PA12

    GPIOA->AFRL = ((GPIOA->AFRL & ~(GPIO_AFRL_AFR0)) | (GPIO_AF_MODE1 << GPIO_AFRL_AFR0_Pos)); //PA3
    GPIOA->AFRH = ((GPIOA->AFRH & ~(GPIO_AFRH_AFR12)) | (GPIO_AF_MODE1 << GPIO_AFRH_AFR12_Pos)); //PA12
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  uart sent byte
/// @param  UARTx : x can be 1 or 2 or 3
/// @param  c :uart sent byte
/// @retval None
////////////////////////////////////////////////////////////////////////////////
void uart_send(s8 c)
{
    UART1->TDR = c;
    while(1) {
        if(UART1->ISR & UART_ISR_TX) {
            UART1->ICR = UART_ICR_TX;
            break;
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
/// @brief  uart receive function
/// @param  UARTx:x can be 1 or 2 or 3
/// @retval None
////////////////////////////////////////////////////////////////////////////////
u8 inbyte(UART_TypeDef* UARTx)
{
    u8 temp;
    while(1) {
        if(UART1->ISR & UART_ISR_RX) {
            UART1->ICR = UART_ICR_RX;
            break;
        }
    }
    temp = (u8)UART1->RDR;
    if(temp == 0xd) { //clear  error receive  data
        return 0;
    }
    return temp;
}


////////////////////////////////////////////////////////////////////////////////
/// @brief  uart receive function  test
/// @param  UARTx , x can be 1 or 2 or 3
/// @retval None
////////////////////////////////////////////////////////////////////////////////

void Uart1RxTest(UART_TypeDef* UARTx)
{
    u8 temp;
    temp = inbyte(UARTx);
    if(temp != 0) {

    }
}
/// @}


/// @}

/// @}





